<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'sports:sportsWeight:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="40%"
  >
    <template #title>
      <!-- <Icon :icon="getTitle.icon" class="pr-1 m-1" /> -->
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm">
      <template #weight="{ model, field }">
        <a-input v-model:value="model[field]" suffix="%" />
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSportsSportsWeightForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { SportsWeight, sportsWeightSave, sportsWeightForm } from '/@/api/sports/sportsWeight';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('sports.sportsWeight');
  const { showMessage } = useMessage();
  const record = ref<SportsWeight>({} as SportsWeight);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增单项指标') : t('编辑单项指标'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('指标分类'),
      field: 'indexType',
      component: 'Select',
      componentProps: {
        dictType: 's_index_type',
        allowClear: true,
      },
      rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('适配年级'),
      field: 'adaptGrade',
      component: 'Select',
      componentProps: {
        dictType: 'vke_grade',
        allowClear: true,
        mode: 'multiple',
      },
    },
    {
      label: t('单项指标名称'),
      field: 'indexName',
      component: 'Input',
      componentProps: {
        maxlength: 32,
      },
      required: true,
    },
    {
      label: t('单项指标单位'),
      field: 'indexUnit',
      component: 'Select',
      componentProps: {
        dictType: 's_index_unit',
      },
      required: true,
    },
    {
      label: t('是否自定义指标'),
      field: 'customizeIndex',
      component: 'RadioGroup',
      componentProps: {
        dictType: 's_index_customize',
      },
      required: true,
    },
    {
      label: t('权重'),
      field: 'weight',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
      slot: 'weight',
      ifShow: ({ values }) => {
        let state;
        if (values.customizeIndex == 1) {
          state = false;
        } else {
          state = true;
        }
        return !!state;
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 200,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    resetFields();
    setDrawerProps({ loading: true });
    console.log(data);
    const res = await sportsWeightForm(data);
    record.value = (res.sportsWeight || {}) as SportsWeight;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });
  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        indexId: record.value.indexId,
      };
      if (data.weight == '') {
        data.weight = 0;
      }
      const res = await sportsWeightSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
